package com.shop2cn.iapi.agentproducts.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.UpdateProductPicBean;
import com.shop2cn.iapi.agentproducts.service.ImportAgentProductCall;
import com.shop2cn.iapi.agentproducts.service.UpdateProductPicCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
/**
*更新商品主图
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_updateProductPic{
	private static UpdateProductPicBean bean;
	private static UpdateProductPicCall call;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();

	private static IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
	
	// 渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	// 代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品主图");
	}

	@Before
	public void caseUp() {
		bean=new UpdateProductPicBean();
		call=new UpdateProductPicCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_001() {
		Logger.start(true,"更新商品主图-验证正常场-是本系统格式的图片");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setMainPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			List<YmtProductsI> product=ymtProductsIWapper.selectProductByProductId(productId);
			Logger.verifyEquals(bean.getMainPicList().get(0),product.get(0).getsPicUrl() , "验证picUrl");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_001_1() {
		Logger.start(true,"更新商品主图-验证不是本系统格式的图片");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			

            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setMainPicList(Arrays.asList("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1587720278&di=7a33ea4f0d6be28e160e603f65c4ce55&src=http://cdnsfb.soufunimg.com/viewimage/open/0/2020_4/23/M15/29/833faccf-9830-4b47-a9a4-65f8854cfdac/450x338c.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("201", call.getCode1(), "验证code");
			Logger.verifyEquals(true, call.getMessage1().contains("请使用内部图片进行上传"), "验证message");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_001_2() {
		Logger.start(true,"更新商品主图-验证图片数等于最大值-8张");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            
            List<String> pics=new ArrayList<String>();
            for (int i=0;i<8 ;i++) {
            	 pics.add("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667"+i+"_n_w_o.jpg");
			}
           
            
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setMainPicList(pics);
			call.setData(bean);
			call.callService();			
				
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			List<YmtProductsI> product=ymtProductsIWapper.selectProductByProductId(productId);
			Logger.verifyEquals(bean.getMainPicList().get(0),product.get(0).getsPicUrl() , "验证picUrl");
			
			
			List<Map<String, Object>> pictures = ipm.selectPictureByProdIdAndAction0(productId);
			for (int i = 0; i < bean.getMainPicList().size()-1; i++) {
				Logger.comment("验证pictures");
				Logger.verifyEquals(bean.getMainPicList().get(i+1), pictures.get(i).get("sOriUrl").toString(),
						"验证商品图片地址");
				Logger.verifyEquals(true, Integer.parseInt(pictures.get(i).get("iAction").toString()) > -1, "验证商品图片有效");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_001_3() {
		Logger.start(true,"更新商品主图-验证图片数大于最大值-9张");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            
            List<String> pics=new ArrayList<String>();
            for (int i=0;i<9 ;i++) {
            	 pics.add("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667"+i+"_n_w_o.jpg");
			}
            
            bean.setUserId(sellerId);
            bean.setProductId(productId);
            bean.setMainPicList(pics);
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_003() {
		Logger.start(true,"更新商品主图-验证userId不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            
            List<String> pics=new ArrayList<String>();
            for (int i=0;i<9 ;i++) {
            	 pics.add("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667"+i+"_n_w_o.jpg");
			}
            
            bean.setUserId(sellerId+111);
            bean.setProductId(productId);
            bean.setMainPicList(pics);
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");
				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_004() {
		Logger.start(true,"更新商品主图-验证userId为空");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
           // bean.setUserId(sellerId+1111);
            bean.setProductId(productId);
            bean.setMainPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_005() {
		Logger.start(true,"更新商品主图-验证productId不存在");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId+111);
            bean.setMainPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("201", call.getCode1(), "验证code");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_006() {
		Logger.start(true,"更新商品主图-验证productId为空");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
           // bean.setProductId(productId);
            bean.setMainPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_007() {
		Logger.start(true,"更新商品主图-验证productId与userId不匹配");
		try {
			ImportAgentProductCall importagentproductCall=new ImportAgentProductCall();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,sellerId,false);
			importagentproductCall.setData(importAgentProductBean);
			importagentproductCall.callService();

			String agentProductId = importagentproductCall.getJsonObject().get("data").getAsJsonObject().get("newProductId").getAsString();
			
            bean.setUserId(agentUserId1);
            bean.setProductId(productId);
            bean.setMainPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("201", call.getCode1(), "验证code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_updateProductPic_008() {
		Logger.start(true,"更新商品主图-验证mainPicList为空");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                    sellerName, false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            bean.setUserId(sellerId);
            bean.setProductId(productId);
           // bean.setMainPicList(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("400", call.getCode1(), "验证code");
			Logger.verifyEquals(true, call.getMessage1().contains("商品主图至少需要上传一张"), "验证message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}